package com.example.ssmxuankeback.annotation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class ValueInIntegersValidator implements ConstraintValidator<ValueInIntegers, Integer> {
    
    private int[] integerList;

    private boolean allowNull;
    
    @Override
    public void initialize(ValueInIntegers constraintAnnotation) {
        integerList = constraintAnnotation.value();
        allowNull = constraintAnnotation.allowNull();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value == null) {
            return allowNull;
        }
        for (Integer integer : integerList) {
            if (integer.equals(value)) {
                return true;
            }
        }
        return false;
    }
}
